package com.longjianghuasheng.logistics.business.inquiry.adapter

import android.annotation.SuppressLint
import android.view.View
import android.widget.CheckBox
import android.widget.FrameLayout
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.business.inquiry.FirstNode
import com.longjianghuasheng.logistics.business.inquiry.TYPE_0

class RootNodeProvider(var block: (FirstNode, Int, Boolean) -> Unit) : BaseNodeProvider() {


    override val itemViewType: Int = TYPE_0

    override val layoutId: Int = R.layout.item_server0

    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        item as FirstNode
        helper.setText(R.id.tv_title, item.title)
        helper.setImageResource(
            R.id.iv_status,
            if (item.isExpanded) R.mipmap.ic_unfold else R.mipmap.ic_shrink
        )
        val checkBox = helper.getView<CheckBox>(R.id.check_box)
        checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
            item.isSelect = checkBox.isChecked
            if (!buttonView.isPressed) return@setOnCheckedChangeListener
            block.invoke(item, helper.absoluteAdapterPosition, isChecked)
        }
        helper.getView<FrameLayout>(R.id.fl_check).setOnClickListener {
            checkBox.isPressed = true
            checkBox.isChecked = !checkBox.isChecked
        }
        checkBox.isChecked = item.isSelect

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        super.onClick(helper, view, data, position)
        getAdapter()?.expandOrCollapse(position)
    }
}